import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:mall/entity/home_entity.dart';
import 'package:mall/utils/navigator_util.dart';
import 'package:mall/widgets/cached_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ProductView extends StatelessWidget {
  List<Goods> productList;

  ProductView(this.productList);
  ScrollController _scrollController = new ScrollController();

  @override
  Widget build(BuildContext context) {
//    return Container(
//      padding: EdgeInsets.all(2.0),
//      child: StaggeredGridView.countBuilder(
//          shrinkWrap: true,
//          itemCount: productList.length,
//          physics: NeverScrollableScrollPhysics(),
//          crossAxisCount: 4,
//          crossAxisSpacing: 4,
//          mainAxisSpacing: 10,
//          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
//              crossAxisCount: 2, childAspectRatio: 0.90),
//          itemBuilder: (BuildContext context, int index) {
//            return _getGridViewItem(context, productList[index]);
//          }),
//    );
    return StaggeredGridView.countBuilder(
        shrinkWrap: true,
        controller: _scrollController,
        crossAxisCount: 4,
        crossAxisSpacing: 1,
        mainAxisSpacing: 1,
        itemCount: productList.length,
        itemBuilder: (context, index) {
          return Container(
              decoration: new BoxDecoration(
                //背景Colors.transparent 透明
                color: Colors.transparent,
                //设置四周圆角 角度
                borderRadius: BorderRadius.all(Radius.circular(4.0)),
                //设置四周边框
                border: new Border.all(width: 1, color: Color(0xFFFFFFFF)),
              ),
              child: new Center(
                child: _getGridViewItem(context, productList[index]),
              ));
        },
        staggeredTileBuilder: (index) =>
            StaggeredTile.count(2, index == 0 ? 2.5 : 3));
  }

  _goGoodsDetail(BuildContext context, Goods goods) {
    NavigatorUtils.goGoodsDetails(context, goods.id);
  }

  Widget _getGridViewItem(BuildContext context, Goods productEntity) {
    return Container(
      child: InkWell(
        onTap: () => _goGoodsDetail(context, productEntity),
        child: Card(
          elevation: 0,
          margin: EdgeInsets.all(1.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                  margin: EdgeInsets.all(1.0),
                  child: CachedImageView(
                      ScreenUtil().setHeight(400.0),
                      ScreenUtil().setHeight(310.0),
                      productEntity.picUrl)),
              Padding(
                padding: EdgeInsets.only(top: 1.0),
              ),
              Container(
                padding: EdgeInsets.only(left: 1.0, top: 1.0),
                alignment: Alignment.centerLeft,
                child: Text(
                  productEntity.name,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(color: Colors.black54, fontSize: 14.0),
                ),
              ),
              Container(
                padding: EdgeInsets.only(left: 1.0, top: 1.0),
                alignment: Alignment.centerLeft,
                child: Text(
                  "￥${productEntity.retailPrice}",
                  style: TextStyle(
                    color: Colors.red,
                    fontSize: 18.0,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
